package com.example.controller;


import cn.hutool.core.io.FileUtil;
import com.example.context.BaseContext;
import com.example.entity.Script;
import com.example.mapper.ScriptMapper;
import com.example.result.PageResult;
import com.example.result.Result;
import com.example.service.IScriptService;
import com.example.utils.CommonControllers;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陈昌吉
 * @since 2024-04-16
 */

@ResponseBody
@CrossOrigin
@Data
@Slf4j
@RestController
@RequestMapping("/script")
public class ScriptController {

    @Autowired
    private IScriptService scriptService;
    /**
     * Scrip上传文件
     * @param file
     * @throws IOException
     */
    @PostMapping("/file")
    public Result<List<Script>> transferFile(MultipartFile file,@RequestParam Integer hid) throws IOException {
        List<Script> insert = scriptService.insert(file,hid);
        return Result.success(insert);
    }

    /**
     * 根据hid(作业id)和login_id(学生id)获取所有Script
     * @return
     */
    @PostMapping("/getAllScrip")
    public Result<List<Script>> getAllScrip(@RequestBody Integer hid){
        log.info("额外{},",hid);
        List<Script> all=  scriptService.getAll(hid);
        return Result.success(all);
    }

    /**
     * 删除script中的url
     * @param script
     * @return
     */
    @DeleteMapping("/deleteUrl")
    public Result removeFile(@RequestBody Script script){
        log.info("文件为,{}",script);
        String s = scriptService.deleteFile(script);
        return Result.success(s);
    }


    //审核员处

    /**
     * 审核员处获取所有的作业和分页
     * @param script
     * @return
     */
    @PostMapping("/getScrip")
    public Result<PageResult> getScrip(@RequestBody Script script){
        log.info("审核员处{},",script);
        PageResult scrip = scriptService.getScrip(script);
        return Result.success(scrip);
    }

    /**
     * 审核对提交的作业进行审核
     * @param script
     * @return
     */
    @PostMapping("updateState")
    public Result  updatesState(@RequestBody Script script){
        log.info("审核后的状态为:{},",script);
      scriptService.updateState(script);
        return Result.success();
    }

}
